document.addEventListener('DOMContentLoaded', function() {
  let saveButton = document.getElementById('saveButton');
  let renameInput = document.getElementById('renameInput');

  // 获取本地化字符串
  let placeholder = chrome.i18n.getMessage('inputPrompt');
  let saveButtonLabel = chrome.i18n.getMessage('saveButton');
  renameInput.placeholder = placeholder;
  saveButton.textContent = saveButtonLabel;
  
  // 重命名操作
  function renameTab() {
    console.debug('Action triggered');
    const newTitle = renameInput.value;
    console.debug('New title: ' + newTitle);
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      let tab = tabs[0];
      console.debug('Tab ID: ' + tab.id);
      chrome.tabs.sendMessage(tab.id, { action: 'changeTitle', newTitle: newTitle });
    });
    // 操作完成后，关闭弹窗
    window.close();
  }

  saveButton.addEventListener('click', renameTab);

  renameInput.addEventListener('keydown', function(e) {
    if (e.key === "Enter") {
      renameTab();
    }
  });
});
